package com.matrix.quartz.action;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.anotations.SaveRequestToken;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.constance.SystemMessageCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.quartz.bean.QuartzJobDefinition;
import com.matrix.quartz.contance.QuartzContance;
import com.matrix.quartz.core.QuartzCoreUtil;
import com.matrix.quartz.dao.QuartzExecuteHistoryDao;
import com.matrix.quartz.dao.QuartzJobDefinitionDao;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @description 定时任务控制器
 * @author zhangheng
 * @date 2019-01-14 11:03
 */
@Controller
@RequestMapping(value = "developer/quartzJobDefinition")
public class QuartzJobDefinitionAction {

	@Autowired
	private QuartzJobDefinitionDao quartzJobDefinitionDao;

	@Autowired
	private QuartzExecuteHistoryDao quartzExecuteHistoryDao;

	@Autowired
	private QuartzCoreUtil quartzCoreUtil;

	
	public static final String BEV = "QuartzJobDefinition_BEV";

	/**
	 * 列表查询
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 20190116
	 * @param quartzJobDefinition
	 * @param pageVo
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(QuartzJobDefinition quartzJobDefinition, PaginationVO pageVo) {

		List<QuartzJobDefinition> dataList = quartzJobDefinitionDao.selectInPage(quartzJobDefinition, pageVo);
		AjaxResult result = new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList,
				quartzJobDefinitionDao.selectTotalRecord(quartzJobDefinition));
		return result;
	}

	/**
	 *添加定时任务
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 20190116
	 * @param quartzJobDefinition
	 * @return
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/addQuartzJobDefinition")
	public @ResponseBody AjaxResult addQuartzJobDefinition(QuartzJobDefinition quartzJobDefinition) {
		quartzJobDefinition.setCreateBy(QuartzContance.USER_NAME);
		quartzJobDefinition.setUpdateBy(QuartzContance.USER_NAME);
		boolean flag = quartzCoreUtil.createCronJob(quartzJobDefinition);
		if (flag) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "定时任务");
		} else {
			throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
		}
	}

	/**
	 * 编辑定时任务
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 20190116
	 * @param newQuartzJobDefinition
	 * @return
	 */
	@RemoveRequestToken
	@RequestMapping(value = "/modifyQuartzJobDefinition")
	public @ResponseBody AjaxResult modifyQuartzJobDefinition(QuartzJobDefinition newQuartzJobDefinition) {
		QuartzJobDefinition oldQuartzJobDefinition = WebUtil.getSessionAttribute(BEV);
		// 定时任务修改  可以修改cron表达式
		try {
			quartzCoreUtil.modifyJob(oldQuartzJobDefinition, newQuartzJobDefinition);
		} catch (SchedulerException e) {
			LogUtil.error("定时任务修改异常", e);
			return new AjaxResult(AjaxResult.STATUS_FAIL, SystemMessageCode.UPDATE_SUCCES, "定时任务");
		}
		WebUtil.removeSessionAttribute(BEV);
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemErrorCode.DATA_UPDATE_FAIL, "定时任务");

	}

	/**
	 * 进入编辑界面
	 */
	@SaveRequestToken
	@RequestMapping(value = "/editForm")
	public ModelAndView editForm(Integer id) {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("developer/quartz/jobDefinition-form");
		QuartzJobDefinition quartzJobDefinition = new QuartzJobDefinition();
		modelAndView.addObject("obj", quartzJobDefinition);
		if (id != null) {
			quartzJobDefinition = quartzJobDefinitionDao.selectById(id);
			// WebUtil.getRequest().setAttribute("obj", quartzJobDefinition);
			modelAndView.addObject("obj", quartzJobDefinition);
			WebUtil.setSessionAttribute(BEV,	 quartzJobDefinition);
		}
		return modelAndView;
	}

	/**
	 * 删除定时任务
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 20190116
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		List<String> ids = StringUtils.strToCollToString(keys, ",");
		// 查询出对应的定时任务
		List<QuartzJobDefinition> quartzJobDefinitionlist = quartzJobDefinitionDao.selectByJdIdList(ids);
		boolean flag = false;
		if (CollectionUtils.isNotEmpty(quartzJobDefinitionlist)) {
			for (QuartzJobDefinition jobDefinition : quartzJobDefinitionlist) {
				try {
					flag = quartzCoreUtil.deleteJob(jobDefinition);
				} catch (SchedulerException e) {
					LogUtil.error("删除定时任务失败#{}", jobDefinition.getJdId());
					return new AjaxResult(AjaxResult.STATUS_FAIL, SystemErrorCode.DATA_DELETE_FAIL);
				}
			}
			// i = quartzJobDefinitionDao.deleteByIds(ids);
		}
		if (flag) {
			return new AjaxResult(AjaxResult.STATUS_SUCCESS,"删除成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_FAIL, SystemErrorCode.DATA_DELETE_FAIL);
		}
	}

	/**
	 * 历史展示
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 20190115
	 * @param jdId
	 * @return
	 */
	@RequestMapping(value = "/showHistory")
	public ModelAndView showHistory(String jdId) {
		ModelAndView modelAndView = new ModelAndView("developer/quartz/jobExecuteHistory-list");
		modelAndView.addObject("jdId", jdId);
		return modelAndView;
	}

	/**
	 * 暂停定时任务
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @param jdId
	 * @return
	 */
	@RequestMapping(value = "/pauseJob")
	@ResponseBody
	public AjaxResult pauseJob(Integer jdId) {
		// 先查询
		QuartzJobDefinition jobDefinition = quartzJobDefinitionDao.selectById(jdId);
		try {
			quartzCoreUtil.pauseJob(jobDefinition);
		} catch (SchedulerException e) {
			LogUtil.error("暂停定时任务失败#{}", jdId.toString());
			return new AjaxResult(AjaxResult.STATUS_FAIL, "暂停失败");
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, "暂停成功");
	}

	/**
	 * 恢复定时任务
	 * 
	 * @author zhangheng
	 * @email 512061637@qq.com
	 * @date 20190117
	 * @param jdId
	 * @return
	 */
	@RequestMapping(value = "/resumeJob")
	@ResponseBody
	public AjaxResult resumeJob(Integer jdId) {
		// 查询对应任务
		QuartzJobDefinition jobDefinition = quartzJobDefinitionDao.selectById(jdId);
		try {
			quartzCoreUtil.resumeJob(jobDefinition);
		} catch (SchedulerException e) {
			LogUtil.error("恢复任务失败#{}", jdId.toString());
			return new AjaxResult(AjaxResult.STATUS_FAIL, "恢复任务失败");
		}
		return new AjaxResult(AjaxResult.STATUS_SUCCESS, "恢复成功");
	}

}